package news.paper.tintuc.tinnong.docbao.doctin.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import news.paper.tintuc.tinnong.docbao.doctin.GCM.MD5;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

public class ApiParams {
	private Map<String, String> mListParams = new HashMap<String, String>();
	private ArrayList<NameValuePair> mListParamsNameValues = new ArrayList<NameValuePair>();
	private String URL = "http://appmanage.tienich360.com/Service/AppManageService.asmx/";
	private String MYKEY = "MYKEY21f5666aad3646fab1f59d7e1cd8c579";
	private String APPKEY = "db780cb22a1e4b97ad16436afd8ae506";
	public ApiParams(String URL) {
		this.URL = URL;
	}
	public ApiParams() {
	}
	
	public void setURL(String uRL) {
		URL = uRL;
	}
	public void init(){
		String time = "NP"+System.currentTimeMillis() + "";
		MD5 md5 = new MD5();
		String requeststring = md5.md5(MYKEY + APPKEY + time);
		
		BasicNameValuePair valuePair = new BasicNameValuePair("requestid", time);
		mListParamsNameValues.add(valuePair);
		mListParams.put("requestid", time);
		
		BasicNameValuePair valuePair2 = new BasicNameValuePair("requeststring", requeststring);
		mListParamsNameValues.add(valuePair2);
		mListParams.put("requeststring", requeststring);
	}
	public ApiParams addParams(String key, String value) {
		BasicNameValuePair valuePair = new BasicNameValuePair(key, value);
		mListParamsNameValues.add(valuePair);
		mListParams.put(key, value);
		return this;
	}

	public ApiParams addParams(ApiParams params) {
		mListParams.putAll(params.getmListParams());
		mListParamsNameValues.addAll(params.getmListNameValueParams());
		return this;
	}

	public Map<String, String> getmListParams() {
		return mListParams;
	}

	public ArrayList<NameValuePair> getmListNameValueParams() {
		return mListParamsNameValues;
	}

	@Override
	public String toString() {
		String data = URL;
		for (int i = 0; i < mListParamsNameValues.size(); i++) {
			NameValuePair namevalue = mListParamsNameValues.get(i);
			data += (i < mListParamsNameValues.size() - 1) ? namevalue
					.getName() + "=" + namevalue.getValue() + "&" : namevalue
					.getName() + "=" + namevalue.getValue();
		}
		return data;
	}
}
